﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAL
{
    public class TUKHOADICHVUDAO : DAOBASE
    {
        public DataTable Search2(Search input)
        {
            DataTable final = new DataTable();
            String sql = "SELECT  DISTINCT MaDichVu  FROM TUKHOADICHVU WHERE INSTR('" + input.InPut + "', TuKhoaDichVu) > 0";
            final = ExcuteQuery(sql);
            return final;
        }   
        public DataTable Search(Search input)
        {
            DataTable final = new DataTable();
            try
            {
                //thuc hien 
                String[] s = Until.ConvertStringToArray2(input.InPut);
                DataColumn[] cl = new DataColumn[] { new DataColumn("MaDichVu") };
                final.Columns.AddRange(cl);
                for (int i = 0; i < s.Length; i++)
                {
                    String sql = "SELECT  DISTINCT MaDichVu  FROM TUKHOADICHVU WHERE ";
                    int pos = 0;
                    if (i + 3 < s.Length)
                    {
                        pos += i + 3;
                    }
                    else
                    {
                        if (i + 3 == s.Length)
                        {
                            pos = s.Length - i;
                            if (pos < i)
                            {
                                pos = s.Length - 1;
                            }
                        }
                        if (i + 3 > s.Length)
                        {
                            pos = s.Length - 1;
                        }

                    }
                    sql +=Until.CreateWhere2(s, "TuKhoaDichVu", i, pos);
                    //sql += Until.CreateWhere(s, "TuKhoaDichVu");
                    DataTable dt = new DataTable();
                    dt = ExcuteQuery(sql);
                    if (dt.Rows.Count >= 0)
                    {
                        foreach (DataRow dr in dt.Rows)
                        {
                            Until.AddData(dr, ref final, "MaDichVu", "MaDichVu");
                        }
                    }
                }
            }
            catch
            {
                return null;
            }
            return final;
            
        }
        public DataTable Search3(ref Search input)
        {
            DataTable final = new DataTable();
            DataTable final1 = new DataTable();
            DataColumn[] cl = new DataColumn[] { new DataColumn("MaDichVu") };
            final1.Columns.AddRange(cl);
            String sql = "SELECT  DISTINCT MaDichVu,tukhoadichvu  FROM TUKHOADICHVU WHERE INSTR('" + input.InPut + "', TuKhoaDichVu) > 0";
            final = ExcuteQuery(sql);
            if (final.Rows.Count >= 0)
            {
                DataRow row = final1.NewRow();
                row["MaDichVu"] = final.Rows[0]["MaDichVu"].ToString();
                final1.Rows.Add(row);
                int dodai = 0; string chuoi = "";
                foreach (DataRow dr in final.Rows)
                {
                    
                    string temp = dr["tukhoadichvu"].ToString().ToLower();
                    if (input.InPut.Contains(temp))
                    {
                        if (dodai < temp.Length)
                            chuoi = temp;
                    }
                }
                input.InPut=input.InPut.Replace(chuoi, "");
            }
            return final1;
        }  
    }
}
